package cn.sumpu.app.wlcity.dao.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import cn.sumpu.app.wlcity.dao.ContentDao;
import cn.sumpu.app.wlcity.domain.Content;
import cn.sumpu.app.wlcity.exception.AppBusinessException;

public class ContentDaoImpl extends SqlMapClientDaoSupport implements ContentDao {

	private static Logger logger = LoggerFactory.getLogger(ContentDaoImpl.class);

	@SuppressWarnings("unchecked")
	@Override
	public List<Content> getContentList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (List<Content>) getSqlMapClientTemplate().queryForList(
					"content.getContentList",map);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("Read database failure.", e);
			}
			throw new AppBusinessException("Read database failure.", e);
		}
	}

	@Override
	public void insertContent(Content content) throws AppBusinessException {
		try {
			getSqlMapClientTemplate().insert(
					"content.insertContent",content);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	@Override
	public Content getContentById(String identifier)
			throws AppBusinessException {
		try {
			return  (Content) getSqlMapClientTemplate().queryForObject(
					"content.getContentById",identifier);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("Read database failure.", e);
			}
			throw new AppBusinessException("Read database failure.", e);
		}
	}

	@Override
	public void updateContent(Content content) throws AppBusinessException {
		try {
			getSqlMapClientTemplate().update(
					"content.updateContent",content);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("update database failure.", e);
			}
			throw new AppBusinessException("update database failure.", e);
		}
		
	}

	@Override
	public void moveContentSort(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlMapClientTemplate().update(
					"content.moveContentSort",map);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("update database failure.", e);
			}
			throw new AppBusinessException("update database failure.", e);
		}
		
	}

	@Override
	public int getContentListCount(Map<String, Object> map) throws AppBusinessException {
		try {
			return (Integer) getSqlMapClientTemplate().queryForObject(
					"content.getContentListCount",map);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("read database failure.", e);
			}
			throw new AppBusinessException("read database failure.", e);
		}
	}

	@Override
	public void deleteContent(String identifier) throws AppBusinessException {
		try {
			getSqlMapClientTemplate().delete(
					"content.deleteContent",identifier);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("update database failure.", e);
			}
			throw new AppBusinessException("update database failure.", e);
		}
	}

	@Override
	public void upEachContentSort(String contentType) throws AppBusinessException {
		try {
			getSqlMapClientTemplate().update(
					"content.upEachContentSort",contentType);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("update database failure.", e);
			}
			throw new AppBusinessException("update database failure.", e);
		}
		
	}

	@Override
	public void downEachVoteSort(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlMapClientTemplate().update(
					"content.downEachContentSort",map);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("update database failure.", e);
			}
			throw new AppBusinessException("update database failure.", e);
		}
		
	}


	

}
